/*
 * 文件名：AdminPayBillServiceImpl.java	 
 * 时     间：下午12:20:21
 * 作     者：lyf       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cz073586.tools.util.Tools;
import com.local.dao.mapers.AdminCarInBillMapper;
import com.local.dao.mapers.LocalEquipmentMapper;
import com.local.dao.mapers.LocalStationMapper;
import com.local.entity.ex.AdminCarInBillEx;
import com.local.model.AdminCarInBill;
import com.local.model.AdminCarInBillExample;
import com.local.model.LocalStation;
import com.local.page.AdminCarInBillPage;
import com.local.service.face.AdminCarInBillService;
import com.local.service.face.CrossingService;
import com.local.service.face.LogService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.BeanUtil;
import com.tt.system.dao.mapers.UserMapper;
import com.tt.system.model.User;
import common.cz0735.coustomer.mapers.CoustomerInfoMapper;
import common.cz0735.coustomer.model.CoustomerInfo;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: AdminPayBillServiceImpl
 * @author Administrator
 * @date 2015年9月26日 下午12:20:21
 */
@Service
public class AdminCarInBillServiceImpl extends
		BaseServiceImpl<AdminCarInBillExample, AdminCarInBill, String> implements
		AdminCarInBillService
{
	@Autowired
	private AdminCarInBillMapper adminCarInBillMappeer;
	
	@Autowired
	private CoustomerInfoMapper coustomerInfoMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private LocalStationMapper localStationMapper;
	
	@Autowired
	private LocalEquipmentMapper locaEqmentMaper;
	
	@Autowired
	private CrossingService crossingServer;
	
	@Autowired
	private LogService logService;
	
	@Override
	public Integer addAdminCarInBill(AdminCarInBill adminCarInBill)
			throws BaseException
	{
		try
		{
			adminCarInBill.setAttr1("手动开闸");
			adminCarInBill.setAttr2("true");//开闸标记，true开闸
			adminCarInBillMappeer.insertSelective(adminCarInBill);
			//开闸
			crossingServer.openGATE(adminCarInBill.getCrossingId(),adminCarInBill.getStationUuid());
			//PushAdminRunble push = new PushAdminRunble(adminPayBill, list.get(0).getExchangeNo(), list.get(0).getCameraNo());
			//ThreadPoolUtil.instance().execute(push);
			
		}catch(Throwable t)
		{
			throw new BaseException(t.getMessage(), t);
		}
		return null;
	}
	
	/**
	 * 分页查询进场开闸信息
	 * 方法描述：
	 * @Title: queryAdminPayBillByPage 
	 * @param @param page
	 * @param @return
	 * @return List<AdminPayBill>
	 * @user Administrator
	 * 2015年10月2日
	 * @throws
	 */
	@Override
	public List<AdminCarInBill> queryAdminCarInBillByPage(
			AdminCarInBillPage<AdminCarInBill> page){
		
		try
		{
			Integer recordCount = adminCarInBillMappeer.countAllByPage(page);
			page.setTotalRecords(recordCount);
			List<AdminCarInBill> list = adminCarInBillMappeer.queryByPage(page);
			
			// 转换岗亭id为岗亭名
			List<AdminCarInBill> listEx = new ArrayList<AdminCarInBill>();
			
			for (AdminCarInBill paybill : list)
			{
				AdminCarInBillEx ex = new AdminCarInBillEx();
				BeanUtil.copyProperties(ex, paybill);
				//岗亭对象
				LocalStation station = localStationMapper.selectByPrimaryKey(paybill
						.getStationUuid());
				if (!Tools.isEmpty(station))
				{
					ex.setStateName(station.getStationName());
				}
				
				//登录用户
				//		
				CoustomerInfo coustomerInfo = coustomerInfoMapper.selectByPrimaryKey(paybill
						.getLonginUserUuid());
				
				if(!Tools.isEmpty(coustomerInfo))
				{
					ex.setLoginUserName(coustomerInfo.getName());
				}else{
					User user = userMapper.selectByPrimaryKey(paybill.getLonginUserUuid());
					ex.setLoginUserName(user.getLoginName());
				}
				
				
				
				listEx.add(ex);
			}
			
			page.setData(listEx);
			return listEx;
		} catch (Exception e)
		{
			throw new BaseException("", e);
		}
	}
	
	
	/**
	 * 根据登录用户查询开闸的记录数
	 * 方法描述：
	 * @Title: queryAdminPayBillCountByLoginUserId 
	 * @param @param loginId
	 * @param @return
	 * @return Integer
	 * @user Administrator
	 * 2015年10月4日
	 * @throws
	 */
	public Integer queryAdminCarInBillCountByLoginUserId(AdminCarInBill adminCarInBill)
	{
		
		return adminCarInBillMappeer.querycountAdminCarInBill(adminCarInBill);
	}
	
	@Override
	public BaseMapper<AdminCarInBillExample, AdminCarInBill, String> getMapper()
	{
		
		return adminCarInBillMappeer;
	}
	
}
